const Administor = require('../modules/Administor');
const GetTime = require('../../../libs/GetTime.js');
const { json } = require('express');
const { log } = require('console');

let BackstageController = {
    //显示后台登录界面
    get_login: function (req, res) {
        res.type('html');
        this.fetch(req, res, 'login');
    },

    // 验证后台登录信息
    post_login: function (req, res) {
        let data = req.body;
        // console.log(data);
        req.session.aname = req.body.username;
        // console.log(req.session.aname);
        let resultDate = Administor.checkLogin(data);
        resultDate.then(
            result => {
                req.session.token = result.token;
                res.send(result);
            }
        )
    },

    // 退出登录
    get_logout: function (req, res) {
        req.session.destroy()
        res.send({
            code: 200,
            msg: '退出成功',
            url: '/backstage/login'
        })
    },

    //显示后台主页
    get_index: function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        this.fetch(req, res, 'index', { aname });
    },

    //显示欢迎页面
    get_welcome: async function (req, res) {
        res.type('html');
        let aname = req.session.aname;
        let timeDate = GetTime.getNowFormatDate();
        let count = await Administor.getDataCount();
        this.fetch(req, res, 'welcome',{ 
            aname, 
            timeDate,
            count
        });
    },

    //显示用户管理页面
    get_user: function (req, res) {
        res.type('html');
        this.fetch(req, res, 'user');
    },

    //显示菜单管理页面
    get_menu: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/menu/menu');
    },

    //显示添加菜单页面
    get_menuAdd: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/menu/menu-add');
    },

    //显示编辑菜单页面
    get_menuEdit: function (req, res) {
        res.type('html');
        let menuName = req.query.obj;
        let menuId = req.query.id;
        this.fetch(req, res, '/menu/menu-edit',{
            menuName,
            menuId
        });
    },

    //显示菜品管理页面
    get_food: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/food/food');
    },

    //显示添加菜品页面
    get_foodAdd: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/food/food-add');
    },

    //显示编辑菜品页面
    get_foodEdit: function (req, res) {
        res.type('html');
        let objstr = req.query.obj;
        objstr = objstr.split('\'').join('\"');
        let food = JSON.parse(objstr);
        this.fetch(req, res, '/food/food-edit',{
            food
        });
    },

    //显示餐桌类型管理页面
    get_tabletype: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/tabletype/tabletype');
    },

    //显示添加餐桌类型页面
    get_tabletypeAdd: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/tabletype/tabletype-add');
    },

    //显示编辑餐桌类型页面
    get_tabletypeEdit: function (req, res) {
        res.type('html');
        let objstr = req.query.obj;
        objstr = objstr.split('\'').join('\"');
        let tabletype = JSON.parse(objstr);
        this.fetch(req, res, '/tabletype/tabletype-edit',{
            tabletype
        });
    },

    //显示餐桌管理页面
    get_table: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/table/table');
    },

    //显示添加餐桌页面
    get_tableAdd: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/table/table-add');
    },

    //显示编辑餐桌页面
    get_tableEdit: function (req, res) {
        res.type('html');
        let objstr = req.query.obj;
        objstr = objstr.split('\'').join('\"');
        let table = JSON.parse(objstr);
        this.fetch(req, res, '/table/table-edit',{
            table
        });
    },

    //显示排队管理页面
    get_row: function (req, res) {
        res.type('html');
        
        this.fetch(req, res, '/row');
    },

    //显示订单管理页面
    get_order: function (req, res) {
        res.type('html');
        this.fetch(req, res, '/order/order');
    },

    //显示编辑订单页面
    get_orderEdit: function (req, res) {
        res.type('html');
        let objstr = req.query.obj;
        objstr = objstr.split('\'').join('\"');
        let order = JSON.parse(objstr);
        this.fetch(req, res, '/order/order-edit',{
            order
        });
    },

    //显示编辑订单页面
    get_orderFood: function (req, res) {
        res.type('html');
        let selectId = req.query.obj;
        this.fetch(req, res, '/order/order-food',{
            selectId
        });
    }
}

BackstageController.__proto__ = require('../../common/BaseController')
module.exports = BackstageController